﻿namespace Practices.SignalR {
    using Microsoft.AspNet.SignalR.Client;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public partial class SendMessagePage : System.Web.UI.Page {
        HubConnection _con;
        IHubProxy _hub;
        protected void Page_Load(object sender, EventArgs e) {
            string url = "http://appuep.erp.cnpc";
            string ip = GetWebPageResponse(url + "/ip.aspx");
            var querystringData = new Dictionary<string, string> { { "uepuid", "Webservice" } };
            var con = new HubConnection(url, querystringData);
            var hub = con.CreateHubProxy("TaskHub");
            con.Start().Wait();
            hub.Invoke("NotifyRecv", new object[] { "86171992", "F6-35-54-DC-9A-E2-6B-66-1B-F7-AE-EB-84-86-33-DA" }).Wait();
            con.Stop();
        }

        public void SendFromServer(string from, string to, string message) {
            var querystringData = new Dictionary<string, string>();
            querystringData.Add("name", from);

            _con = new HubConnection("http://localhost:52527", querystringData);
            _hub = _con.CreateHubProxy("chatHub");
            _con.Start().Wait();
            _hub.Invoke("Send", new object[] { from, to, message }).Wait();
            _con.Stop();
        }

        static string GetWebPageResponse(string uriArg) {
            Stream responseStream = WebRequest.Create(uriArg).GetResponse().GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);
            return reader.ReadToEnd();
        }
    }
}